﻿#r @"..\Microsoft.SharePoint.Client (spo)\Microsoft.SharePoint.Client.dll"
#r @"..\Microsoft.SharePoint.Client (spo)\Microsoft.SharePoint.Client.Runtime.dll"

#load "Client.fs"

open Client
open Microsoft.SharePoint.Client
open Microsoft.SharePoint.Client.Utilities
open Microsoft.SharePoint.Client.Sharing
open Microsoft.SharePoint.Client.Application
open System.Security;

// =======================================================
// Work with folders and items in folders
// =======================================================

let web = Client.Context.Web;
Client.Load web

let list = web.Lists.GetByTitle("Documents")
Client.Load list

let folder = web.GetFolderByServerRelativeUrl("/Documents/TestFolder")
Client.Load folder

folder.ServerRelativeUrl
folder.ServerObjectIsNull

let item = list.GetItemById(15)
Client.Load item

for fld in item.FieldValues do
    let mutable msg = fld.Key 
    if fld.Value = null then
        msg <- fld.Key
    else 
        msg <- fld.Key + "|" + fld.Value.ToString()
    printfn "%s\n" msg
   
// Retriebe all items within a folder and subfolders
let query = new CamlQuery()
query.ViewXml <- "<View Scope='RecursiveAll'><Query><Where><Eq><FieldRef Name='FileRef'/><Value Type='File'>/Documents/TestFolder/Folder321</Value></Eq></Where></Query></View>"
let items = list.GetItems(query)
Client.Load items

items.Count